package com.mt.mtyx.product.controller;

import com.mt.mtyx.common.exception.BusinessException;
import com.mt.mtyx.common.result.Result;
import com.mt.mtyx.product.service.FileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传
 *
 * @author WMT
 * @Date 2023/7/20 18:00
 */
@Api(tags = "文件上传")
@RequestMapping("/admin/product")
@RestController
public class FileUploadController {

    @Autowired
    private FileUploadService fileUploadService;

    @ApiOperation("上传文件")
    @PostMapping("/fileUpload")
    public Result<String> upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (file == null || file.isEmpty()) {
            throw new BusinessException("文件不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(fileUploadService.upload(file));
    }
}
